<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Floor;
use App\Models\Household;
use App\Models\Unit;
use Illuminate\Http\Request;
use App\Models\Community;
use Illuminate\Support\Facades\DB;
use test\Mockery\Fixtures\EmptyTestCaseV5;


class CommunityController extends Controller
{
    /*
     * 小区列表
     */
    public function lists(Request $request, Community $community)
    {
        $propertyId = $request->input('property_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $res = $community->indexPageLike($request->page, $request->limit, $request->condition, $propertyId);
        } else {
            $res = $community->indexPage($request->page, $request->limit, $propertyId);
        }
        if ($res && count($res) > 0) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 小区数量
     */
    public function listNum(Request $request, Community $community)
    {
        $propertyId = $request->input('property_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $condition = $request->condition;
            $query = 'SELECT COUNT(*) as count FROM `community`  WHERE `property_id`=? AND CONCAT(`community_name`,`community_address`,`community_developers`,`community_mobile`,`community_person`,`community_person_mobile`) LIKE ?';
            $param = '%' . $condition . '%';
            $count = DB::select($query, array($propertyId, $param));
            $res = $count[0]->count;
        } else {
            $res = $community->count();
        }
        if ($res) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 创建小区
     * */
    public function save(Request $request, Community $community)
    {
        $code = $community->create($request->all());
        if ($code) {
            return $this->return_json(200, '', '添加成功');
        } else {
            return $this->return_json(201, '', '添加失败');
        }
    }

    /*
     * 删除小区
     * */
    public function delete_Community(Request $request, Community $community)
    {
        $this->validate($request,
            ['id' => 'required'],
            ['id.required' => 'id不能为空']);
        $ids = $request->id;
        $ids = is_array($ids) ? $ids : (is_string($ids) ? explode(',', $ids) : func_get_args());
        $code = $community->destroy($ids);
        if (!empty($code)) {
            return $this->return_json(200, '', '删除成功');
        } else {
            return $this->return_json(201, '', '删除失败');
        }
    }

    /*
     * 修改小区
     * */
    public function update_Community(Request $request, Community $community)
    {
        $this->validate($request,
            ['id' => 'required'],
            ['id.required' => 'id不能为空']);
        $id = $request->id;
        $code = $community->whereid($id)->update($request->all());
        if (!empty($code)) {
            return $this->return_json(200, '', '修改成功');
        } else {
            return $this->return_json(201, '', '修改失败');
        }
    }

    public function getCommunityById(Request $request, Community $community)
    {
        $this->validate($request,
            ['id' => 'required'],
            ['id.required' => 'id不能为空']);
        $res = $community->whereid($request->id)->get();
        if ($res && !$res->isEmpty()) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 住户表
     * */
    public function household_list(Request $request, Floor $fllor)
    {
        $page = $request->page;
        $limit = $request->limit;
        $communityId = $request->input('community_id', 1);
        $start = ($page - 1) * $limit;
        if (isset($request->condition) && !empty($request->condition)) {
            $condition = $request->condition;
            $query = 'SELECT * FROM `floor` WHERE `community_id`=? AND CONCAT(`unit`,`number`,`layer_number`,`households`) LIKE ? limit ?,?';
            $param = '%' . $condition . '%';
            $res = DB::select($query, array($communityId, $param, $start, $limit));
            json_decode(json_encode($res), true);
        } else {
            $res = $fllor->offset($start)->limit($limit)->get();
        }
        if ($res && count($res) > 0) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询数据为空');
        }
    }

    /*
    * 住户表记录数
    * */
    public function household_list_num(Request $request, Floor $fllor)
    {
        $communityId = $request->input('community_id', 1);
        if (isset($request->condition) && !empty($request->condition)) {
            $condition = $request->condition;
            $query = 'SELECT COUNT(*) as count FROM `floor` WHERE `community_id`=? AND CONCAT(`unit`,`number`,`layer_number`,`households`) LIKE ?';
            $param = '%' . $condition . '%';
            $count = DB::select($query, array($communityId, $param));
            $res = $count[0]->count;
        } else {
            $res = $fllor->count();
        }
        if ($res) {
            return $this->return_json(200, $res, '查询成功');
        } else {
            return $this->return_json(201, '', '查询失败');
        }
    }

    /*
     * 创建栋数 单元 层数 门牌号  创建住户表
     * */
    public function add_list_Storied(Request $request, Floor $fllor, Unit $unit)
    {
//      $request->community_id;//小区ID 绑定小区id
//      $request->floor;//楼号 栋数
//      $request->unit;//单元
//      $request->layer_number;//层数
//      $request->households;//户数
        $fllor->community_id = $request->community_id;
        $fllor->number = $request->floor;
        $data = ['community_id' => $fllor->community_id, 'number' => $request->floor,
            'comment' => $request->floor . '栋', 'households' => $request->households, 'unit' => $request->unit,
            'layer_number' => $request->layer_number
        ];

        $fllor_res = $fllor->create($data);//添加栋数
        $res = ['floor_id' => $fllor_res->id, 'number' => $request->unit, 'comment' => $request->unit . '单元'];
        $unit->create($res);//添加单元 绑定栋数
        $data = [];
        for ($i = 1; $i <= $request->layer_number; $i++) {//先循环层数
            $a = $i;
            for ($x = 1; $x <= $request->households; $x++) {//在循环户数
//              dump($i . '楼' . $i . '0' . $x);
                $data['house_number'] = $request->floor . '-' . $request->unit . '-' . $i . '0' . $x;//门排号
                $data['floor'] = $i;//所在楼层
                $data['floor_number'] = $x;//所在楼层 号数
                $data['unit_id'] = $request->unit;//所属单元id
                $data['community_id'] = $request->community_id;//存入小区id
                $data['door_number'] = $request->floor . $request->unit . $i . '0' . $x;//门牌号缩写用作查找
                $data['floor_don'] = $request->floor;//栋数
                $data['unit'] = $request->unit;//单元
                DB::table('household')->insert($data);//执行循环添加
            }
        }
        return $this->return_json(200, '', '创建成功');
    }

    /*
     * 删除住户表
     * */
    public function delete_household(Request $request, Floor $floor, Unit $unit, Household $household)
    {
        $this->validate($request, [
            'id' => 'required|integer'
        ], [
            'id.required' => 'id不能为空',
            'id.integer' => 'id必须是数字'
        ]);
        $res = $floor->whereid($request->id)->get()->toArray();
        if (!empty($res)) {
            $date = $unit->where('floor_id', '=', $request->id)->get()->toArray();
            $floor->whereid($request->id)->delete();
            if (!empty($date)) {
                $household->whereunit_id($date[0]['id'])->delete();
                $unit->where('floor_id', '=', $request->id)->delete();
                return $this->return_json(200, '', '删除成功');
            }
        }
    }

    public function communityList()
    {

        $data = Community::select('id', 'community_name')->get();
        return $this->return_json(200, $data, '查询成功');
    }
}
